<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="box">
        <input type="password" placeholder="请输入6-16位" id="input">

        <button id="button">显示</button>
        <p class="color">请输入6-16位</p>
    </div>
    <script>
        //1.获取元素
        var password = document.getElementById('input')
        var button = document.getElementById('button')
        var color = document.querySelector(".color")
        //绑定事件
        var flag = 0
        button.onclick = function () {
            if (flag == 0) {
                password.type = 'text'
                button.innerHTML = "隐藏"
                flag = 1
            } else {
                password.type = 'password'
                button.innerHTML = "显示"
                flag = 0
            }
        }
        //失去焦点事件
        password.onblur = function () {
            if (this.value.length < 6 || this.value.length > 16) {
                // console.log(color);
                color.className = "color colorRed"
                color.innerHTML = "输入的位数不对"
            } else {
                color.className = "color green"
                color.innerHTML = "输入z正确"
            }
        }
    </script>
    <style>
        #input::placeholder {
            color: rgb(38, 0, 255);
        }

        .colorRed {
            color: red;
        }

        .green {
            color: #0f0;
        }
    </style>
</body>

</html>